package com.helios.news.service;

import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.helios.core.service.ServiceAdapter;
import com.helios.news.bean.News;
import com.helios.news.bean.Notice;
import com.helios.news.mapper.NoticeMapper;

@Service
public class NoticeService extends ServiceAdapter<Notice> {
	
	@Autowired
	private NoticeMapper noticeMapper;
	
    @Override
    protected String getDispInfo() {
        return "公告";
    }

    @Override
    protected String getCacheKey(Object args) {
        if(args instanceof News) {
            return "NOTICE_" + ((News) args).getId();
        } else {
            return "NOTICE_" + args.toString();
        }
    }
    
    public Notice get(Integer id) {
        return getCache(id, Notice.class, (key) -> noticeMapper.get(NumberUtils.toInt(key.toString())));
    }
    
    public List<Notice> gets(Integer page, Integer rows) {
        int index = (page - 1) * rows;
        if (index < 0) index = 0;
        return noticeMapper.gets(index, rows);
    }
    
    public int getCount() {
        return noticeMapper.getCount();
    }
    
    public boolean save(Notice bean) {
        if(bean == null) return false;
        if(bean.getId() == null || bean.getId() == 0) {
            return noticeMapper.insert(bean) == 1;
        } else {
            return updateCache(bean, (obj) -> noticeMapper.update(obj) == 1);
        }
    }
    
    public boolean delete(Integer id) {
        return deleteCache(id, (key) -> noticeMapper.delete(key) == 1);
    } 

}
